package com.szcinda.service.dto.line;

import com.szcinda.repository.Line;
import com.szcinda.repository.LineRepository;
import com.szcinda.service.SnowFlakeFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;


@Service
@Transactional
public class LineServiceImpl implements LineService {

    private final LineRepository lineRepository;
    private final SnowFlakeFactory snowFlakeFactory;

    public LineServiceImpl(LineRepository lineRepository) {
        this.lineRepository = lineRepository;
        this.snowFlakeFactory = SnowFlakeFactory.getInstance();
    }

    @Override
    public void create(LineCreateDto codeCreateDto) {
        Line record = lineRepository.findByOriginPlaceAndDestPlace(codeCreateDto.getOriginPlace(), codeCreateDto.getDestPlace());
        Assert.isTrue(record == null, "已经存在此线路");
        record = new Line();
        BeanUtils.copyProperties(codeCreateDto, record);
        record.setId(snowFlakeFactory.nextId("LI"));
        lineRepository.save(record);
    }

    @Override
    public void delete(String id, String userId) {
        Line record = lineRepository.findFirstById(id);
        lineRepository.delete(record);
        /*User user = userRepository.findById(userId);
        LogCreateDto createDto = new LogCreateDto(user.getId(), user.getNickName(), "删除业务编号规则", record.getName());
        logService.create(createDto);*/
    }

    @Override
    public List<Line> findAll(LineQueryDto params) {
        Specification<Line> specification = ((root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (!StringUtils.isEmpty(params.getOriginPlace())) {
                Predicate originPlace = criteriaBuilder.like(root.get("originPlace").as(String.class), "%" + params.getOriginPlace().trim() + "%");
                predicates.add(originPlace);
            }
            if (!StringUtils.isEmpty(params.getDestPlace())) {
                Predicate originPlace = criteriaBuilder.like(root.get("destPlace").as(String.class), "%" + params.getDestPlace().trim() + "%");
                predicates.add(originPlace);
            }
            if (!StringUtils.isEmpty(params.getBusinessType())) {
                Predicate originPlace = criteriaBuilder.equal(root.get("businessType").as(String.class), params.getBusinessType());
                predicates.add(originPlace);
            }
            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
        });
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        return lineRepository.findAll(specification, sort);
    }

    @Override
    public List<Line> search(LineQueryDto params) {
        return lineRepository.search(params.getSearchText());
    }

    @Override
    public void batchCreate(List<LineImportDto> importDatas, String opId) {
        List<Line> vehicles = lineRepository.findAll();
        for (LineImportDto importData : importDatas) {
            boolean b = vehicles.stream().noneMatch(line -> line.getOriginPlace().equals(importData.getOriginPlace()) && line.getDestPlace().equals(importData.getDestPlace()));
            if (b) {
                Line line = new Line();
                BeanUtils.copyProperties(importData, line);
                line.setId(snowFlakeFactory.nextId("LI"));
                lineRepository.save(line);
            }
        }
    }
}
